package com.mmp.main.servlet.device;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mmp.common.bo.Message;
import com.mmp.common.servlet.BasicServlet;
import com.mmp.common.util.Common;
import com.mmp.common.util.FileUtil;
import com.mmp.main.entity.HospitalDevice;
import com.mmp.main.entity.HospitalDevicePpmDetail;
import com.mmp.main.util.Default;
import com.mmp.main.util.WebUtil;

@MultipartConfig
public class PPMVisitProcessServlet extends BasicServlet {

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		int deviceId = WebUtil.getParamValueAsInteger(request, "deviceId", 0);

		if (deviceId == 0) {
			announceErorrMessage(request, response, Default.GENERAL_ERROR_MESSAGE);
			return;
		}

		int ppmId = WebUtil.getParamValueAsInteger(request, "ppmId", 0);

		HospitalDevicePpmDetail ppm = null;

		if (ppmId != 0) // update mode
			ppm = dbQueryManager.findById(ppmId, HospitalDevicePpmDetail.class);
		else
			ppm = new HospitalDevicePpmDetail();

		HospitalDevice device = new HospitalDevice();
		device.setId(deviceId);
		ppm.setHospitalDevice(device);

		ppm.setVisitMonth(WebUtil.getParamValueAsInteger(request, "visitMonth", null));
		ppm.setVisitYear(WebUtil.getParamValueAsInteger(request, "visitYear", null));
		
		ppm.setSubcontractor(WebUtil.getParamValue(request, "subcontractor", null));

		int visitStatusInt = WebUtil.getParamValueAsInteger(request, "visitStatus", null);
		ppm.setVisitStatus(visitStatusInt);

		// reset values
		ppm.setVisitDate(null);
		ppm.setVisitCost(null);
		ppm.setVisitReportUrl(null);
		ppm.setDelayPenalty(null);
		ppm.setAbsenceReason(null);
		ppm.setAbsenceReportUrl(null);

		if (visitStatusInt == 1 || visitStatusInt == 3) { //in time or late
			ppm.setVisitDate(WebUtil.getParamValueAsDate(request, "visitDate", null));
			ppm.setVisitCost(WebUtil.getParamValueAsFloat(request, "visitCost", null));
		//	ppm.setSubcontractor(WebUtil.getParamValue(request, "subcontractor", null));
			String[] visitReportURL = FileUtil.uploadFiles(request, "visitReportURL");
			// if (visitReportURL != null && visitReportURL.length != 0)
			ppm.setVisitReportUrl(Common.concatenateValues(visitReportURL));

			if (visitStatusInt == 3) {
				ppm.setDelayPenalty(WebUtil.getParamValueAsFloat(request, "delayPenalty", null));
			}

		} else if (visitStatusInt == 2) { //

			ppm.setAbsenceReason(WebUtil.getParamValue(request, "absenceReason", null));

			String[] absenceReportURL = FileUtil.uploadFiles(request, "absenceReportURL");
			// if (absenceReportURL != null && absenceReportURL.length != 0)
			ppm.setAbsenceReportUrl(Common.concatenateValues(absenceReportURL));

		}

		ppm.setCreatedBy(getSessionUser(request));
		ppm.setCreatedIn(new Date());

		ppm = dbQueryManager.mergeEntity(ppm);

		if (ppmId == 0)
			setMessage(request, new Message("تمت الاضافة بنجاح", Message.MESSAGE_TYPE.SUCCESS));
		else
			setMessage(request, new Message("تم التعديل بنجاح", Message.MESSAGE_TYPE.SUCCESS));

		sendRedirect(request, response, "PpmFollowupSearch");

	}
}
